import java.util.Arrays;

/**
 * Vector
 */
public class Vector {
    private float[] items;

    public Vector(float... items ) {
        this.items = items;
    }

    /**
     * 长度
     * @return
     */
    public float length(){
        float power = 0.0f;
        for(int i = 0; i < items.length; i++){
            power += Math.pow(items[i], 2);
        }
        return (float) Math.sqrt(power);
    }

    /**
     * 标量 * 向量
     * @param num 标量值
     * @return rt
     */
    public Vector multiNum(float num){
        for(int i = 0; i < items.length; i++){
            items[i] *= num;
        }
        return this;
    }

    /**
     * dot:  P·Q=P1*Q1+P2*Q2+...
     * @param v2 v2
     * @return  rt
     */
    public float dot(Vector v2){
        if(this.items.length != v2.items.length){
            throw new RuntimeException("维度不一样，无法点乘");
        }

        float result = 0.0f;
        for(int i = 0; i < this.items.length; i++){
            result += this.items[i] * v2.items[i];
        }
        return result;
    }

    /**
     * 点乘结果和向量两平面夹角关系
     * P·Q=||P||*||Q||*cosα
     * cosα = P·Q / ||P||*||Q||
     */
    public float getAngleDeg(Vector v2){
        float dotRt = dot(v2);
        double angleRb = Math.acos(dotRt / (length() * v2.length()));
        return (float) Math.toDegrees(angleRb);
    }

    /**
     * 投影计算  Pproj = (P·Q / ||Q||^2) * Q
     * @param v2 v2
     * @return rt
     */
    public Vector getProj(Vector v2){
        float dotRt = dot(v2);
        float lenPow = (float) Math.pow(v2.length(), 2);
        //dotRt / lenPow
        return v2.multiNum(dotRt / lenPow);
    }


    @Override
    public String toString() {
        return Arrays.toString(items);
    }
}
